package org.glut.friendsService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.glut.friendsService.service.IFriendsService;
import org.glut.friendsService.entity.Friends;
import org.glut.friendsService.mapper.FriendsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务实现类
 *
 * @author qjc
 * @since 2024-08-16 12:29
 */
@Service
public class FriendsServiceImpl extends ServiceImpl<FriendsMapper, Friends> implements IFriendsService {

    @Autowired
    private FriendsMapper friendMapper;

    @Override
    public List<Friends> getFriendsByUserId(Integer userId) {
        QueryWrapper<Friends> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return friendMapper.selectList(queryWrapper);
    }

    @Override
    public boolean addFriend(Integer userId, Integer friendId) {
        // 检查是否已存在好友关系
        QueryWrapper<Friends> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("friend_id", friendId);
        if (friendMapper.selectCount(queryWrapper) > 0) {
            return false; // 好友已存在
        }

        // 添加好友
        Friends friend = new Friends();
        friend.setUserId(userId);
        friend.setFriendId(friendId);
        return friendMapper.insert(friend) == 1;
    }

    @Override
    public boolean deleteFriend(Integer userId, Integer friendId) {
        QueryWrapper<Friends> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("friend_id", friendId);
        return friendMapper.delete(queryWrapper) == 1;
    }
}
